package com.example.demo.controller;
import com.example.demo.entity.OrderEntity;
import com.example.demo.entity.TimeEntity;
import com.example.demo.entity.UserEntity;
import com.example.demo.service.OrderService;
import com.example.demo.service.TimeService;
import com.example.demo.util.RunTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private TimeService timeService;


    @RequestMapping("selectBySidAndStime")
    @ResponseBody
    public List<OrderEntity> selectBySidAndStime (OrderEntity orderEntity){
        System.out.println(orderEntity);
      return orderService.selectBySidAndStime(orderEntity);
    }
    @RequestMapping("addUidOrder")
    @ResponseBody
    public int addOrder(OrderEntity orderEntity){
        TimeEntity stime = orderEntity.getStime();
        TimeEntity timeEntity = timeService.selectByTimeid(stime.getTimeid());
        String time = timeEntity.getTime();

        String[] split = time.split("-");
        String[] split1 = split[1].split("：");
        int i1 = Integer.parseInt(split1[0]);

        Date date = new Date();
        int hours = date.getHours();

        if (i1 <= hours){
            return 2;
        }else {
            int i = orderService.insertOrder(orderEntity);
            System.out.println(orderEntity);
            if (i>0){
                return 1;
            }else{
                return 0;
            }
        }
    }

    @RequestMapping("RunTime")
    @ResponseBody
    public void RunTimeupdate(OrderEntity orderEntity){
        int timeid = orderEntity.getStime().getTimeid();
        TimeEntity timeEntity = timeService.selectByTimeid(timeid);
        String time2 = timeEntity.getTime();
        String[] split = time2.split("-");
        String[] split1 = split[1].split("：");
        int i1 = Integer.parseInt(split1[0]);
//        int time=i1*3600;
        int endTime=i1;
        RunTimeUtil runTimeUtil = new RunTimeUtil();
        int i2 = runTimeUtil.RunTimeYuYue(endTime);
        if (i2==0){
            OrderEntity orderEntity1 = new OrderEntity();
            orderEntity1.setUid(orderEntity.getUid());
            orderEntity1.setStime(orderEntity.getStime());
            OrderEntity orderEntity2 = orderService.selectByUidOrder(orderEntity1);
            orderEntity2.setOflag(1);
            orderService.updateOrder(orderEntity2);
        }
    }

    @RequestMapping("selectByUidOrder")
    @ResponseBody
    public int selectByUidOrder(OrderEntity orderEntity){
        OrderEntity orderEntity1 = orderService.selectByUidOrder(orderEntity);
        if (orderEntity1!=null){
            return 1;
        }else{
            return 0;
        }
    }
    @RequestMapping("toAllOrder")
    public String toAllOrder (Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        UserEntity userEntity = (UserEntity)session.getAttribute("userEntity");
        System.out.println("userEntity="+userEntity);
        List<OrderEntity> orderEntities = orderService.selectUidOrder(userEntity.getUid());
        System.out.println("orderEntities="+orderEntities);
        model.addAttribute("userOrder",orderEntities);
        return "userorder-list";
    }
    @RequestMapping("updataOflagByOid")
    @ResponseBody
    public int updataOflagByOid(OrderEntity orderEntity){
        orderEntity.setOflag(1);
        System.out.println(orderEntity);
        int i = orderService.updateOrder(orderEntity);
        if (i>0){
            return 1;
        }else{
            return 0;
        }
    }
    @RequestMapping("adminAllOrder")
    public String adminAllOrder(Model model){
        model.addAttribute("adminAllOrder",orderService.adminAllOrder());
        return "adminorder-list";
    }
}